package com.shd.edanpei.image;

import android.content.Context;
import android.support.v4.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;


/**
 * 采用懒加载模式 使用的时候再去初始化Glide
 */
public class LoadImageCtrl implements ILoadImgs {

    private static final String TAG = LoadImageCtrl.class.getSimpleName();

    private Context mContext;
    private Fragment mFragment;
    private RequestManager mImgRequestMgr;

    public LoadImageCtrl(Context mContext) {
        this.mContext = mContext;
    }

    public LoadImageCtrl(Fragment mFragment) {
        this.mFragment = mFragment;
    }

    private boolean initGlide() {
        if (null != mImgRequestMgr) {
            return true;
        }
        if (null != mFragment) {
            mImgRequestMgr = Glide.with(mFragment);
            return true;
        }
        if (null != mContext) {
            mImgRequestMgr = Glide.with(mContext);
            return true;
        }
        return false;
    }

    @Override
    public void onStart() {
        if (null != mImgRequestMgr) {
            mImgRequestMgr.onStart();
        }
    }

    @Override
    public void onStop() {
        if (null != mImgRequestMgr) {
            mImgRequestMgr.onStop();
        }
    }

    @Override
    public void onDestroy() {
        if (null != mImgRequestMgr) {
//            mImgRequestMgr.onDestroy();
        }
    }

    @Override
    public RequestManager getGlide() {
        if (initGlide()) {
            return mImgRequestMgr;
        }
        return null;
    }

}
